package spring.boot.btc;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import spring.boot.btc.common.BlockProperty;
import spring.boot.btc.model.*;

import java.util.ArrayList;
import java.util.List;

@EnableScheduling
@SpringBootApplication
public class SpringBootBtcApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootBtcApplication.class, args);
    }


    /**
     * 初始化创世区块，所有账户的创世区块全都一模一样
     */
    @Override
    public void run(String... args) throws Exception {
        if (BlockProperty.blockList.size() > 0) {
            return;
        }

        TransactionInput input = TransactionInput.getFirstInput();
        List<TransactionInput> transactionInputs = new ArrayList<>();
        transactionInputs.add(input);

        TransactionOutput output = new TransactionOutput(input.getPublicKey(), input.getMoney());
        List<TransactionOutput> transactionOutputs = new ArrayList<>();
        transactionOutputs.add(output);

        Transaction transaction = new Transaction(transactionInputs, transactionOutputs);

        Block block = new Block("0", 0L, 0L, transaction, 1577808000000L);

        BlockProperty.blockList.add(block);
    }
}
